#include "comma-operator.h"

#include <headings.h>

#include <iostream>

using namespace std;

static void show_1();
static void show_2();
static void show_3();

void show_comma_operator()
{
    HEADING1("Comma operator demo");
    show_1();
    cout << "\n\n";
    show_2();
    cout << "\n\n";
    show_3();
}

static void show_1()
{
    HEADING2("Basic effect of the comma operator");
    cout << "  i = 1,5;\n";
    cout << "  yields this:\n";

    int i;

    i = 1,5;
    cout << "  i == " << i << endl;
    cout << "  (compiled with warnings enabled this should "
        "produce a warning like \"5 is a garbage statement\")\n";
}

static void show_2()
{
    HEADING2("A list of comma-separaated statements has the value of the last");
    cout << "  k = (i=1, j=2);\n";
    int i, j, k;

    k = (i=1, j=2);
    cout << "i == " << i << '\n';
    cout << "j == " << j << '\n';
    cout << "k == " << k << '\n';
}

static void show_3()
{
    HEADING2("Usefulness is doubted; here a for loop with 2 initializers");
    int i, j;

    for (i=0, j=10; i<10; i++, j--)
        cout << i << ' ' << j << endl;
}

